<template>
  <div>
    <bread second="用户管理" third="用户列表"></bread>
    <el-card class="box-card" style="width:100%;margin-top:15px">
      <div style="margin-top: 15px;">
        <!-- input输入搜索框 -->
        <el-input
          placeholder="请输入内容"
          v-model="form.query"
          class="input-with-select"
          style="width:400px;margin-right:20px"
          clearable
          @clear="getUserList"
        >
          <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
        </el-input>
        <el-button type="primary" @click="dialogFormVisible = true">添加用户</el-button>
        <!-- 表格 -->
        <el-table :data="tableData" border style="width: 100%;margin-top:15px">
          <el-table-column type="index" label="#" width="60px"></el-table-column>
          <el-table-column prop="username" label="姓名" width="80px"></el-table-column>
          <el-table-column prop="email" label="邮箱"></el-table-column>
          <el-table-column prop="mobile" label="电话"></el-table-column>
          <el-table-column prop="role_name" label="角色"></el-table-column>
          <el-table-column prop="type" label="状态" width="80px">
            <template slot-scope="scoped">
              <el-switch
                v-model="scoped.row.mg_state"
                active-color="#409eff"
                inactive-color="#ff4949"
                @change="setStatus(scoped.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-setting"></el-button>
            <el-button type="warning" size="mini" icon="el-icon-delete"></el-button>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="form.pagenum"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="form.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <!-- 添加用户弹框 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="formName">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" autocomplete="off" style="width:500px;"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" autocomplete="off" style="width:500px;"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" autocomplete="off" style="width:500px;"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile" autocomplete="off" style="width:500px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUsers('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getUserList_api, addUsers_api, setStatus_api } from "@/api/index.js";
export default {
  data() {
    return {
      //验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "长度为11个字符", trigger: "blur" },
        ],
      },
      value: true,
      tableData: [], //表格数据
      form: {
        //获取用户信息列表
        query: "",
        pagenum: 1,
        pagesize: 10,
        //添加用户信息数据
        username: "",
        passwoed: "",
        email: "",
        mobile: "",
      },
      total: null,
      dialogFormVisible: false,
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    //获取用户列表信息
    async getUserList() {
      const { data: res } = await getUserList_api(this.form);
      //   console.log(res, 1);
      if (res.meta.status === 200) {
        this.$message({
          message: res.meta.msg,
          type: "success",
        });
        this.tableData = res.data.users;
        this.total = res.data.total;
      }
    },
    //添加用户
    addUsers(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { data: res } = await addUsers_api(this.form);
          //   console.log(res, 2);
          if (res.meta.status === 201) {
            this.$message({
              message: res.meta.msg,
              type: "success",
            });
            this.getUserList();
            this.dialogFormVisible = !this.dialogFormVisible;
            this.$refs[formName].resetFields();
          }
        } else {
          return;
        }
      });
    },
    //修改用户状态
    async setStatus(row) {
      const { data: res } = await setStatus_api({
        uId: row.id,
        type: row.type,
      });
      if (res.meta.status === 200) {
        this.$message({
          message: res.meta.msg,
          type: "success",
        });
      }
    },
    //分页
    handleSizeChange(val) {
      this.form.pagesize = val;
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.form.pagenum = val;
      console.log(`当前页: ${val}`);
    },
  },
};
</script>
<style lang="less" scoped>
</style>